import Vue from 'vue'
// 引入插件
import Vuex from 'vuex'

// 应用插件
Vue.use(Vuex)

const actions = {
  // add(context, value) {
  //   console.log('diaoyongle actions add', context, value)
  //   context.commit('saveAdd', value)
  // },
  // plus(context, value) {
  //   context.commit('savePlus', value)
  // },

  addOdd(context, value) {
    if (context.state.num % 2) {
      context.commit('saveAdd', value)
    }
  },
  addWait(context, value) {
    setTimeout(() => {
      context.commit('saveAdd', value)
    }, 500)
  }
}

const mutations = {
  saveAdd(state, value) {
    console.log('saveAdd', state, value)
    state.num += value;
  },
  savePlus(state, value) {
    state.num -= value;
  },
  saveUser(state, value) {
    state.userList.unshift(value)
  }
}

const state = {
  userName: 'Zoe',
  subject: '前端',
  num: 0,
  userList: []
}

const getters = {
  bigNum(state) {
    return state.num * 10
  }
}

export default new Vuex.Store({
  actions,
  mutations,
  state,
  getters
})